<template>
  <div>
    <div>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55">
      </el-table-column>
      <el-table-column :label="$t('源IP')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.src }}</template>
      </el-table-column>
      <el-table-column :label="$t('目的IP')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.des }}</template>
      </el-table-column>
      <el-table-column :label="$t('源端口')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.sport }}</template>
      </el-table-column>
      <el-table-column :label="$t('目的端口')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.dport }}</template>
      </el-table-column>
      <el-table-column :label="$t('协议')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.prot }}</template>
      </el-table-column>
      <el-table-column :label="$t('剩余时间')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.time }}</template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { getDynamicBlacklist, delDynamicBlack } from '@/api/safe/blacklist'
export default {
  name: 'DynamicBlacklist',
  data () {
    return {
      tableData: [],
      addrStr: '',
      timer: null
    }
  },
  created () {
    this.listData()
  },
  beforeDestroy () {
    if (this.timer) {
      clearInterval(this.timer) // 在Vue实例销毁前，清除我们的定时器
    }
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      var addrStr = ''
      for (var item of data) {
        if (addrStr === '') {
          addrStr = item.id
        } else {
          addrStr = addrStr + '@#@' + item.id
        }
      }
      this.addrStr = addrStr
    },
    // 列表
    listData () {
      let _that = this
      if (this.timer) {
        clearInterval(this.timer)
      }
      getDynamicBlacklist().then(res => {
        if (res.status === 0) {
          this.tableData = res.info
          _that.timer = setInterval(() => {
            for (let item of this.tableData) {
              if (item.time > -1) {
                item.time -= 1
              } else if (item.time === 0) {
                setTimeout(() => {
                  _that.listData()
                }, 1000)
              }
            }
          }, 1000)
        }
      })
    },
    deleteData () {
      if (this.addrStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delDynamicBlack({ id: this.addrStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.addrStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    }
  }
}
</script>
